package cn.com.css.taxInspection.gt3.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import cn.com.css.taxInspection.common.constants.Constants;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.common.exception.BusinessException;
import cn.com.css.taxInspection.common.exception.code.BaseResponseCode;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.mapper.DmGySwjgMapper;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.system.vo.res.DeptRespNodeVO;
import cn.com.css.taxInspection.util.TsBqUtils;

@Service
public class DmGySwjgServiceImpl extends ServiceImpl<DmGySwjgMapper, DmGySwjgEntity> implements DmGySwjgService {

    @Resource
    private HttpSessionService httpSessionService;

    @Resource
    private DmGySwjgMapper dmGySwjgMapper;

    @Resource
    private DmGySwjgService dmGySwjgService;

    @Override
    public List<DeptRespNodeVO> deptTreeList(String deptId) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        List<DmGySwjgEntity> list;
        DmGySwjgEntity dmGySwjgEntity = this.getById(currentDeptId);
        //上级税务机关代码
        String sjswjgdm;
        List<DmGySwjgEntity> dmGySwjgEntityList;
        if ("12101009000".equals(currentDeptId)) {
            sjswjgdm = "12101009000";
            dmGySwjgEntityList = dmGySwjgService.selectQsSwjgById(sjswjgdm);
        } else {
            sjswjgdm = dmGySwjgEntity.getSjswjgDm();
            dmGySwjgEntityList = TsBqUtils.recursionLevelTaxOrg(sjswjgdm);
        }
        List<String> taxOrglist = new ArrayList<>();
        taxOrglist.add(sjswjgdm);
        if (!TsBqUtils.isNull(dmGySwjgEntityList)) {
            for (DmGySwjgEntity dmGySwjg : dmGySwjgEntityList) {
                String swjgDm = dmGySwjg.getSwjgDm();
                taxOrglist.add(swjgDm);
            }
        }
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
        queryWrapper.in(DmGySwjgEntity::getSwjgDm, taxOrglist);
        queryWrapper.orderByAsc(DmGySwjgEntity::getSwjgDm);
        if (TsBqUtils.isEmpty(deptId)) {
            list = dmGySwjgMapper.selectList(queryWrapper);
            for (DmGySwjgEntity swjgEntity : list) {
                if (swjgEntity.getSwjgDm().equals(sjswjgdm)) {
                    swjgEntity.setSjswjgDm("00000000000");
                }
            }
        } else {
            DmGySwjgEntity sjDmGySwjgEntity = this.getById(sjswjgdm);
            if (sjDmGySwjgEntity == null) {
                throw new BusinessException(BaseResponseCode.DATA_ERROR);
            }
            queryWrapper.likeRight(DmGySwjgEntity::getSwjgDm, deptId);
            List<Object> childIds = dmGySwjgMapper.selectObjs(queryWrapper);
            list = dmGySwjgMapper
                    .selectList(Wrappers.<DmGySwjgEntity>lambdaQuery().notIn(DmGySwjgEntity::getSwjgDm, childIds));

        }
        // 默认加一个顶级方便新增顶级部门
        DeptRespNodeVO respNodeVO = new DeptRespNodeVO();
        respNodeVO.setTitle("默认顶级部门");
        respNodeVO.setId("00000000000");
        respNodeVO.setSpread(true);
        respNodeVO.setDisabled(true);
        respNodeVO.setChildren(getTree(list));
        List<DeptRespNodeVO> result = new ArrayList<>();
        result.add(respNodeVO);
        return result;
    }

    //这个是树头
    private List<DeptRespNodeVO> getTree(List<DmGySwjgEntity> all) {
        List<DeptRespNodeVO> list = new ArrayList<>();
        for (DmGySwjgEntity sysDept : all) {
            if ("00000000000".equals(sysDept.getSjswjgDm())) {
                DeptRespNodeVO deptTree = new DeptRespNodeVO();
                deptTree.setId(sysDept.getSwjgDm());
                deptTree.setDeptNo(sysDept.getSwjgDm());
                deptTree.setPid(sysDept.getSjswjgDm());
                deptTree.setTitle(sysDept.getSwjgmc().substring(6));
                deptTree.setSpread(true);
                deptTree.setChildren(getChild(sysDept.getSwjgDm(), all));
                list.add(deptTree);
            }
        }
        return list;
    }

    //这个是树枝
    private List<DeptRespNodeVO> getChild(String id, List<DmGySwjgEntity> all) {
        List<DeptRespNodeVO> list = new ArrayList<>();
        for (DmGySwjgEntity sysDept : all) {
            if ("12101009000".equals(id)) {
                if (sysDept.getSjswjgDm().equals("12101009000") || sysDept.getSjswjgDm().equals("12101009100") || sysDept.getSjswjgDm().equals("12101009200") || sysDept.getSjswjgDm().equals("12101009300")) {
                    DeptRespNodeVO deptTree = new DeptRespNodeVO();
                    deptTree.setId(sysDept.getSwjgDm());
                    deptTree.setDeptNo(sysDept.getSwjgDm());
                    deptTree.setPid(sysDept.getSjswjgDm());
                    deptTree.setTitle(sysDept.getSwjgmc().substring(6));
                    deptTree.setChildren(getChild(sysDept.getSwjgDm(), all));
                    list.add(deptTree);
                }
            } else {
                if (sysDept.getSjswjgDm().equals(id)) {
                    DeptRespNodeVO deptTree = new DeptRespNodeVO();
                    deptTree.setId(sysDept.getSwjgDm());
                    deptTree.setDeptNo(sysDept.getSwjgDm());
                    deptTree.setPid(sysDept.getSjswjgDm());
                    deptTree.setTitle(sysDept.getSwjgmc().substring(6));
                    deptTree.setChildren(getChild(sysDept.getSwjgDm(), all));
                    list.add(deptTree);
                }
            }
        }
        return list;
    }

    @Override
    public String getSjswjgDm(String currentDeptId) {
        return this.getById(currentDeptId).getSjswjgDm();
    }

    @Override
    public DmGySwjgEntity getSwjgDm(String taxcode) {
        return this.getById(taxcode);
    }

    /**
     * 根据税务机关名称查询实体
     */
    @Override
    public DmGySwjgEntity getSwjgDmBySwjgjc(String swjgjc) {
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getSwjgjc, swjgjc);
        queryWrapper.eq(DmGySwjgEntity::getYxbz, Constants.AJZT_LX_Y);
        return dmGySwjgMapper.selectOne(queryWrapper);
    }


    @Override
    public String getSwjgDmBySwjgJc(String swjgDm) {
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getSwjgDm, swjgDm);
        queryWrapper.eq(DmGySwjgEntity::getYxbz, Constants.AJZT_LX_Y);
        return dmGySwjgMapper.selectOne(queryWrapper).getSwjgjc();
    }


    @Override
    public List<DmGySwjgEntity> selectBycurrentDeptId(String currentDeptId) {
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
        queryWrapper.like(DmGySwjgEntity::getSwjgDm, currentDeptId.substring(0, 9));
        queryWrapper.eq(DmGySwjgEntity::getSwjgbz, "1");
        queryWrapper.orderByDesc(DmGySwjgEntity::getSwjgDm);
        return this.list(queryWrapper);
    }

    @Override
    public List<DmGySwjgEntity> selectQsSwjgById(String currentDeptId) {
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
        queryWrapper.like(DmGySwjgEntity::getSwjgDm, currentDeptId.substring(0, 8));
        queryWrapper.eq(DmGySwjgEntity::getSwjgbz, "1");
        queryWrapper.orderByAsc(DmGySwjgEntity::getSwjgDm);
        return this.list(queryWrapper);
    }

    @Override
    public List<DmGySwjgEntity> getDmGyswjgByCurrentMan() {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String substring = currentDeptId.substring(0, 9);
        LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
        queryWrapper.like(DmGySwjgEntity::getSwjgmc, "%沈阳市%稽查%");
        queryWrapper.like(DmGySwjgEntity::getSjswjgDm, substring);
        return this.list(queryWrapper);
    }

}
